/*************************************************************
 * File: GenderType.java
 * Copyright (c) 2012
 * Author: Huang Shaobin
 * Email: Shaobin.Software@gmail.com Shaobin_Software@163.com 
 * Date: Mar 8, 2013
 * Description: 
 ************************************************************/

package snow.core.enums.type;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;

import snow.core.enums.Gender;

/**
 * 性别枚举类型映射类
 * 
 * @author Shaobin.Software@gmail.com Shaobin_Software@163.com
 * @version 1.0
 */
public class GenderType extends EnumUserType {

	/** 从JDBC的ResultSet读取属性值。这个方法是在从数据库查询数据时用到。 */
	public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
		return Gender.toGenderFrom(rs.getObject(names[0]));
	}

	/** 将属性的值设置到PreparedStatement。 */
	public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
		if (value instanceof Gender) {
			st.setObject(index, ((Gender) value).getValue());
		}
	}

	/** 告诉Hibernate这个UserType用来映射的数据类型。这里是Status类 */
	public Class<?> returnedClass() {
		return Gender.class;
	}

	/** 告诉Hibernate要使用什么SQL列类型生成DDL */
	public int[] sqlTypes() {
		return new int[] { Types.BOOLEAN };
	}
}
